/*******************************************************************************
 * Copyright (c) 2014 lifeng (lifengsofts@gmail.com)
 * See the file licenses/license.txt for copying permission.
 ******************************************************************************/
package cn.woblog.android.httputils;

import java.net.HttpURLConnection;

import org.apache.http.HttpResponse;

import android.os.AsyncTask;
import cn.woblog.android.httputils.domain.Request;
import cn.woblog.android.httputils.domain.Request.RequestTool;
import cn.woblog.android.httputils.exception.AppException;
import cn.woblog.android.httputils.exception.AppException.Type;
import cn.woblog.android.httputils.utils.HttpClientUtils;
import cn.woblog.android.httputils.utils.HttpURLConnectUtils;
import cn.woblog.android.httputils.utils.Logs;

public class RequestTask extends AsyncTask<Void, Integer, Object> {
	private static final String TAG = "RequestTask";
	private Request request;
	private int retryCount;

	public RequestTask(Request request) {
		this.request = request;
	}

	@Override
	protected Object doInBackground(Void... params) {
		retryCount = 0;
		return sendRequest();
	}

	private Object sendRequest() {
		if (request.isCancel) {
			this.cancel(true);
			return null;
		}
		try {
			Object result = null;
			if (request.callback != null) {
				result = request.callback.preRequest();
				if (result != null) {
					return result;
				}
			}
			if (request.tool == RequestTool.HTTPCLIENT) {
				HttpResponse response = HttpClientUtils.execute(request);
				// throw new AppException(Type.TimeoutException,
				// "test time out");
				Logs.d(TAG, "request tool:httpclient");
				if (request.callback != null) {
					result = request.callback.handle(response);
					return request.callback.postRequest(result);
				}
			} else {
				Logs.d(TAG, "request tool:httpurlconnection");
				HttpURLConnection connection = HttpURLConnectUtils
						.execute(request);
				if (request.callback != null) {
					result = request.callback.handle(connection);
					return request.callback.postRequest(result);
				}
			}
			return null;
		} catch (AppException e) {
			// e.printStackTrace();
			if (Type.TimeoutException == e.getType()
					&& retryCount < request.mRetryCount) {
				retryCount++;
				Logs.d(TAG, "retry " + retryCount);
				return sendRequest();
			}
			return e;
		}
	}

	@Override
	protected void onPostExecute(Object result) {
		super.onPostExecute(result);
		if (request.isCancel) {
			this.cancel(true);
			return;
		}
		if (request.callback != null) {
			if (result instanceof AppException) {
				AppException tempException = (AppException) result;
				if (Type.CanceledException == tempException.getType()) {
					request.callback.onCanceled();
				} else {
					request.callback.onFailure(tempException);
				}
			} else {
				request.callback.onSuccess(result);
			}
		}
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
	}

	@Override
	protected void onProgressUpdate(Integer... values) {

		super.onProgressUpdate(values);
	}

}
